package Lee_code;

import java.util.*;

/**
 * @author blh
 * @date 2024年07月08日 7:57
 */
public class Lee_958_78 {
    public boolean isCompleteTree(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        // for (int i = 1; i < list.size(); i++) {
        //     if (list.get(i) != list.get(i - 1) + 1) {
        //         return false;
        //     }
        // }
        return deepSearch(root, list);
    }

    private boolean deepSearch(TreeNode root, List<Integer> list) {
        Deque<TreeNode> deque = new LinkedList<>();
        deque.add(root);

        while (!deque.isEmpty()) {
            int size = deque.size();
            for (int i = 0; i < size; i++) {
                TreeNode temp = deque.poll();
                list.add(temp.val);
                if (temp.val != -1) {
                    if (temp.right != null) {
                        deque.offer(temp);
                    } else {
                        deque.offer(new TreeNode(-1));
                    }
                    if (temp.left != null) {
                        deque.offer(temp);
                    } else {
                        deque.offer(new TreeNode(-1));
                    }
                }
            }
        }
        for (int i = 1; i < list.size(); i++) {
            if (list.get(i) == -1 && list.get(i - 1) != -1) {
                return false;
            }
        }
        return true;
    }
}
